# _*_coding=utf-8 _*_
"""
名称：mission_03
版本：v1.0
功能：
（需提交代码作业）使用 requests 库抓'什么值得买'，电脑类的前30名，并将内容保存到本地的一个文件。
时间：2020-12-03
作者：长毛怪^^
"""

import requests
from lxml import etree
from fake_useragent import UserAgent
import pandas as pd


def smzdm_get():
    ua = UserAgent(verify_ssl=False)
    header = {'user-agent': ua.random,
              'Referer': 'https://www.smzdm.com/'}

    url = 'https://www.smzdm.com/fenlei/diannaozhengji/'
    r = requests.get(url, headers=header)

    selector = etree.HTML(r.text)
    name_type = selector.xpath('//div[@class="z-feed-content "]/h5/a/text()')
    money = selector.xpath('//div[@class="z-highlight"]/a/text()')
    link = selector.xpath('//div[@class="z-feed-content "]/h5/a/@href')

    data = pd.DataFrame(index=[i for i in range(30)])
    data['商品名称'] = ''
    data['价格'] = ''
    data['连接'] = ''

    path = './html_get/'
    for i in range(30):
        data['商品名称'].loc[i] = name_type[i]
        data['价格'].loc[i] = money[i].strip().lstrip()
        data['连接'].loc[i] = link[i]
        if data['价格'].loc[i] == '':
            data['价格'].loc[i] = "商品下架过期"

    data.to_excel(path+'什么值得买_电脑类.xlsx')


if __name__ == '__main__':
    smzdm_get()
